package asteroids;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.AdjustmentListener;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.Box.Filler;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class AsteroidsJPanel extends JPanel {
	ArrayList<Bullet> bulletarraylist = new ArrayList<Bullet>();
	Image spaceship;
	Image aster;
	Timer timer;
	
	private int xspaceship, yspaceship;
	private int xasteroid, yasteroid;

	// private Timer timer;
	// CONSTRUCTOR
	public AsteroidsJPanel(int wh, int ht) {

		try {
			URL url = new URL(// "http://www.iphonefreak.com/wp-content/uploads/2010/02/Tea.jpg");
					"http://profile.ak.fbcdn.net/hprofile-ak-snc4/41472_100001644309087_9972_q.jpg");
			ImageIcon img = new ImageIcon(url);

			spaceship = img.getImage();
			xspaceship = (wh / 2 - spaceship.getWidth(null) / 2);
			yspaceship = (ht / 2 - spaceship.getHeight(null) / 2);
		} catch (IOException e) {
			e.printStackTrace();

		}
	
		URL asteroid;
		try {
			asteroid = new URL("http://diablo2.diablowiki.net/images/c/c1/D1-magic-rock.gif");
		
		ImageIcon img2 = new ImageIcon(asteroid);
		
		
		 aster = img2.getImage();
		 xasteroid = (230);
		 yasteroid = (100);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		
		
		AnimationLogic al = new AnimationLogic(this);
		// 1000 makes it call the actionPerformed every second
		timer = new Timer(1, al);
		timer.start();

		AsteroidKeyListener akl = new AsteroidKeyListener(this);
		addKeyListener(akl);

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;

		Toolkit.getDefaultToolkit().sync();

		drawspaceship(g2d);
		spaceshipbullet(g2d);

		Toolkit.getDefaultToolkit().sync();
		g.dispose();

	}

	private void spaceshipbullet(Graphics2D g2d) {
		// looping through and adding as many bullets as many times as long as its less than the array list
		
		for (int i = 0; i < bulletarraylist.size(); i++) {
			int h=bulletarraylist.get(i).getHt();
			int w=bulletarraylist.get(i).getWh();
			int xcord=bulletarraylist.get(i).getX();
			int ycord=bulletarraylist.get(i).getY();
			
			
			Ellipse2D.Double oval = new Ellipse2D.Double(xcord+20, ycord-5, 9, 9);
			g2d.setColor(Color.darkGray);
			g2d.fill(oval);
		}
	}

	private void drawspaceship(Graphics2D g2d) {

		g2d.drawImage(spaceship, xspaceship, yspaceship, null);// THE PRINT OF
		g2d.drawImage(aster, xasteroid, yasteroid, null);														// IMAGES
	}

	public void addBullet() {
		Bullet bullet = new Bullet();
		bullet.setX(xspaceship);
		bullet.setY(yspaceship);

		bulletarraylist.add(bullet);
	}

	public int getXspaceship() {
		return xspaceship;
	}

	public void setXspaceship(int xspaceship) {
		this.xspaceship = xspaceship;
	}

	public int getYspaceship() {
		return yspaceship;
	}

	public void setYspaceship(int yspaceship) {
		this.yspaceship = yspaceship;
	}

	public ArrayList<Bullet> getBulletarraylist() {
		return bulletarraylist;
	}

	public void setBulletarraylist(ArrayList<Bullet> bulletarraylist) {
		this.bulletarraylist = bulletarraylist;
	}

}
